package com.test.project.system.dati.controller;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.test.framework.aspectj.lang.enums.BusinessType;
import com.test.project.system.dajuan.domain.TDajuan;
import com.test.project.system.dajuan.service.ITDajuanService;
import com.test.project.system.dati.domain.TDati;
import com.test.project.system.dati.service.ITDatiService;
import com.test.framework.web.controller.BaseController;
import com.test.framework.web.domain.AjaxResult;
import com.test.common.utils.poi.ExcelUtil;
import com.test.framework.web.page.TableDataInfo;

/**
 * 用户答题Controller
 * 
 * @author test
 * @date 2023-12-05
 */
@Controller
@RequestMapping("/system/dati")
public class TDatiController extends BaseController
{
    private String prefix = "system/dati";
    @Autowired
    private ITDajuanService tDajuanService;

    @Autowired
    private ITDatiService tDatiService;

    @GetMapping()
    public String dati()
    {
        return prefix + "/dati";
    }

    /**
     * 查询用户答题列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TDati tDati)
    {
        startPage();
        List<TDati> list = tDatiService.selectTDatiList(tDati);
        return getDataTable(list);
    }

    /**
     * 导出用户答题列表
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TDati tDati)
    {
        List<TDati> list = tDatiService.selectTDatiList(tDati);
        ExcelUtil<TDati> util = new ExcelUtil<TDati>(TDati.class);
        return util.exportExcel(list, "dati");
    }

    /**
     * 新增用户答题
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户答题
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TDati tDati)
    {
        return toAjax(tDatiService.insertTDati(tDati));
    }
    @PostMapping("/add1")
    @ResponseBody
    public AjaxResult addSave(@RequestBody List<TDati> datis)
    {
    	Long wenjuanId = datis.get(0).getDajuanId();
    	TDajuan tDajuan = new TDajuan();
    	tDajuan.setWenjuanId(wenjuanId);
    	tDajuan.setCreateBy(getLoginName());
    	
    	List<TDajuan> dajuanList = tDajuanService.selectTDajuanList(tDajuan);
    	if(dajuanList.size()>0) {
    		tDajuan = dajuanList.get(0);
    		if(tDajuan.getStatus().equals("0")) {
    			tDajuan.setStatus("1");
    			tDajuan.setCreateTime(new Date());
    			tDajuanService.updateTDajuan(tDajuan);
    		}else {
    			return AjaxResult.error("您已答题！");
    		}
    		for(TDati t : datis) {
        		t.setDajuanId(tDajuan.getId());
        		tDatiService.insertTDati(t);
        	}
    		 return AjaxResult.success();
    	}
    
        return AjaxResult.error();
    }
    /**
     * 修改用户答题
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TDati tDati = tDatiService.selectTDatiById(id);
        mmap.put("tDati", tDati);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户答题
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TDati tDati)
    {
        return toAjax(tDatiService.updateTDati(tDati));
    }

    /**
     * 删除用户答题
     */
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tDatiService.deleteTDatiByIds(ids));
    }
}
